package com.lin.xd.bkprocrastination;

import android.app.AlertDialog;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.text.InputFilter;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class Welcom extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcom);


        Button button = (Button) findViewById(R.id.login);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(Welcom.this);
                 View view = getLayoutInflater().inflate(R.layout.activity_login,null);
                EditText editText = (EditText)view.findViewById(R.id.editText);
                EditText editText1 = (EditText)view.findViewById(R.id.editText1);

                editText1.setMinEms(6);
                editText1.setMaxEms(30);

                final Pattern number = Pattern.compile("\\d+");
                final Pattern pPhone =Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$");
                final Pattern pMail = Pattern.compile("^[\\w&&[^_]]+@[\\w&&[^_]]+\\.com$");
                final Pattern pPass = Pattern.compile("[\\w&&[^_]]+");

                editText1.setFilters(new InputFilter[] {
                        new InputFilter.LengthFilter(30)
                });

                editText1.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                    @Override
                    public void onFocusChange(View v, boolean hasFocus) {
                        if (!hasFocus) {
                            EditText text = (EditText)v;
                            String s = text.getText().toString();
                            if(s.equals("")) {
                                Toast.makeText(Welcom.this,"密码不能为空", Toast.LENGTH_SHORT).show();
                            } else {
                                if(s.length()<6) {
                                    Toast.makeText(Welcom.this,"密码至少为六位",Toast.LENGTH_SHORT).show();
                                } else {
                                    Matcher matcher = pPass.matcher(s);
                                    if(!matcher.matches()) {
                                        Toast.makeText(Welcom.this,"密码只能为数字或字母",Toast.LENGTH_SHORT).show();
                                    }
                                }
                            }
                        }
                    }
                });

                editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                    @Override
                    public void onFocusChange(View v, boolean hasFocus) {
                        if(!hasFocus) {
                            EditText text1 = (EditText)v;
                            String s = text1.getText().toString();
                            if (s.equals("")) {
                                Toast.makeText(Welcom.this,"账号不能为空",Toast.LENGTH_SHORT).show();
                            } else {
                                Matcher mNumber = number.matcher(s);
                                if (mNumber.matches()) {
                                    Matcher mPhone = pPhone.matcher(s);
                                    if(!mPhone.matches()) {
                                        text1.setTextColor(getResources().getColor(R.color.red));
                                        Toast.makeText(Welcom.this,"手机号输入有误",Toast.LENGTH_SHORT).show();
                                    } else {
                                        text1.setTextColor(getResources().getColor(R.color.black));
                                    }
                                } else {
                                    Matcher mMail = pMail.matcher(s);
                                    if(!mMail.matches()) {
                                        text1.setTextColor(getResources().getColor(R.color.red));
                                        Toast.makeText(Welcom.this,"邮箱输入有误",Toast.LENGTH_SHORT).show();
                                    } else {
                                        text1.setTextColor(getResources().getColor(R.color.black));
                                    }
                                }
                            }
                        }
                    }
                });

                builder.setView(view);
                AlertDialog alertDialog = builder.create();
                alertDialog.show();
            }
        });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_welcom, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
